

Let's use reflection to set a bunch of configurable values 
via the command-line using "system properties."

<p>
<a style='text-decoration: none; color: black;' 
href="http://aptframework.dev.java.net/gooey/systemProperties.html">
<img border=0 src="http://weblogs.java.net/blog/evanx/archive/kcoloredit.png" width="32" height="32" 
align="left" hspace="8"/>
<div style="border-bottom: solid 1px; background-color: yellow;">
<span style="font-style: normal; font-weight: bold;"><span style="color: black;">Click here to read "System Properties, the short story"</span>
<br><span style="font-style: italic; color: black;">A part of "Gooey Beans, the GUI part of a trilogy in 42 parts"</span>
</div>
</a>
</p>

<p>
<table>
<tr><td>
<form action='http://aptframework.dev.java.net/gooey/systemProperties.html'> 
<a href='http://aptframework.dev.java.net/gooey/systemProperties.html' 
  style="text-decoration: none;">
<input type=submit value="Read System Properties" />
</a>
</form>
</table>

   

Code Snippet //section 

We put our configurable properties into a separate object, specifying the default values, 
as follows.

<pre class='java'>
public class CommandoDemoProperties {    
    public String host = "aptframework.net";
    public int port = 80;
    public int sslPort = 443;
    public boolean useSSL = true;    
    public String clientKeyStoreFileName = "keystores/client.private";    
    public String serverKeyStoreFileName = "keystores/server.public";    
    ...
}    
</pre>

We create the following helper to inject any command-line parameters via reflection, 
according to the field names, eg. ~-Dport=8080.

<pre class='java'>
public class QSystemPropertyHelper {
    ...
    protected void injectSystemProperties(Object properties) 
    throws IllegalAccessException {
        for (Field field : properties.getClass().getFields()) {
            Class type = field.getType();
            String key = field.getName();
            Object defaultValue = field.get(properties);
            Object value = defaultValue;
            String string = System.getProperty(key);
            if (string == null) continue;
            if (type == String.class) {
                value = string;
            } else if (type == Integer.class || type == int.class) {
                value = Integer.getInteger(key);
            } else if (type == Boolean.class || type == boolean.class) {
                value = Boolean.getBoolean(key);
            } else {
                logger.warning(key, type);
            }
            if (value != defaultValue) {
                field.set(properties, value);
            }
        }
    }    
}
</pre>

where we support properties which are strings, integers or boolean values. 

<br>

<table>
<tr><td>
  <form action='http://aptframework.dev.java.net/gooey/systemProperties.html'> 
    <a href='http://aptframework.dev.java.net/gooey/systemProperties.html' 
      style="text-decoration: none;"><input type=submit value="System Properties" /></a>
  </form>
<td>
  <form action='http://aptframework.dev.java.net/gooey/contents.html'>
  <a href='http://aptframework.dev.java.net/gooey/contents.html' 
  style='text-decoration: none;'><input type=submit value='Gooey Contents'/></a></form>
<td>
  <form action='http://aptframework.dev.java.net/foundation/contents.html'>
    <a href='http://aptframework.dev.java.net/foundation/contents.html'
      style="text-decoration: none;"><input type=submit value='Foundation Trilogy'/></a>
</table>
